<template>
  <div class="upload-container">
    <!-- <el-upload
      :multiple="true"
      :file-list="fileList"
      :show-file-list="false"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :before-upload="beforeUpload"
      class="editor-slide-upload"
      action="https://httpbin.org/post"
      list-type="picture-card"
    >
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload> -->
    <input
      id="fileImport"
      type="button"
      value="上传本地文件"
      style="background: green; height: 80px; width: 100px; font-size: 28px"
      @click="clickLoad"
    >
    <input
      id="files"
      ref="refFile"
      type="file"
      style="display: none"
      @change="fileLoad"
    >
    <!-- <el-dialog :visible.sync="tinymcedialogVisible">
      <el-button @click="tinymcedialogVisible = false"> Cancel </el-button>
      <el-button type="primary" @click="handleSubmit"> Confirm </el-button>
    </el-dialog> -->
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'
import Oss from '@/utils/oss'

export default {
  name: 'EditorSlideUpload',
  props: {
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data() {
    return {
      tinymcedialogVisible: false,
      listObj: {},
      fileList: [],
      stsToken: {
        accessKeyId: 'LTAI5tCQiELrNuXxFdf5wyuB',
        accessKeySecret: 'VlsN1KXHj4RV709wAOIHOnzjn9Qbke',
        bucket: 'utopia9527',
        region: 'oss-cn-shanghai'
      },
      oss: {}
    }
  },
  created() {
    this.oss = new Oss(this.stsToken, window)
    this.oss.__init__()
  },
  methods: {
    clickLoad() {
      // 下面三种方法实现效果一样
      // 方法一: 原生html
      // document.getElementById('files').click();
      // 方法二: jquery实现
      // $("#files").click();
      // 方法三:Vue实现
      this.$refs.refFile.dispatchEvent(new MouseEvent('click'))
    },
    fileLoad() {
      const that = this
      // 获取读取的文件File对象 下面两种方法实现效果一样
      // 方法一:原生html获取
      // const selectedFile = document.getElementById('files').files[0];
      // 方法二:Vue实现
      const selectedFile = this.$refs.refFile.files[0]
      console.log(this.$refs.refFile.files)
      var formData = new FormData()
      var files = $('#testFile')[0].files[0]
      formData.append('File', files)
      formData.append('FileName', fileName)

      that.oss.__main__('activity', 'uploadImg', selectedFile).then((res) => {
        console.log(res)
        if (res.res.status === 200) {
          that.$message.success('上传成功')
          console.log(res)
        }
      })
    },
    checkAllSuccess() {
      return Object.keys(this.listObj).every(
        (item) => this.listObj[item].hasSuccess
      )
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map((v) => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message(
          'Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!'
        )
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.fileList = []
      this.tinymcedialogVisible = false
    },
    handleSuccess(response, file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file
          this.listObj[objKeyArr[i]].hasSuccess = true
          return
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = {
            hasSuccess: false,
            uid: file.uid,
            width: this.width,
            height: this.height
          }
        }
        resolve(true)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}
</style>
